const api = require('../../api/api.js');
const app = getApp();

Page({
  data: {
    isClaimed: false,
    isMe: false,
    msg: '',
    imgUrl: app.globalData.imgUrl + 'placeImage/',
    sensors: [],
    current: 0,
    place: null,
    placeId: 0,
    siteId: 0,
    entId: 0,
    userId: '' 
  },
  onLoad(o) {
    let that = this;
    let placeId = o.placeId;
    let siteId = o.siteId;
    let entId = o.entId;
    wx.getStorage({
      key: 'userId',
      success(res) {
        let userId = res.data;
        that.queryPlaceInfo(placeId);
        that.queryImgs(placeId);
        // that.queryClaim(placeId);
        that.querySensorData(placeId);
        that.setData({
          placeId,
          siteId,
          entId,
          userId
        });
        wx.getStorage({
          key: 'role',
          success(res) {
            if (res.data === null) {
              that.setData({
                isClaimed: true
              })
            } else {
              that.placeIsClaim()
            }
          }
        })
      }
    })
  },
  queryPlaceInfo(placeId) {
    let that = this;
    api.queryPlaceInfo({
      data: {
        placeId,
        userId: ''
      },
      success(res) {
        console.log(res.data);
        let place = res.data.placeInfos[0];
        let goodId = place.good;
        that.setData({
          place
        });
        // api.queryGood({
        //   data: {
        //     id: goodId,
        //     userId: ''
        //   },
        //   success(res) {
        //     console.log(res.data);
        //     let good = res.data.good[0];
        //     place.good = good.name;

        //   }
        // });
      }
    });
  },
  queryImgs(placeId) {
    let that = this;
    api.queryPlaceImage({
      data: {
        placeId
      },
      success(res) {
        console.log(res.data);
        let imgs = res.data.placeImages;
        let images = imgs.map((i) => {
          return i.imgUrl
        });
        that.setData({
          images
        });
      }
    });
  },
  queryClaim(placeId) {
    let that = this;
    api.queryClaim({
      data: {
        placeId,
        userId: ''
      },
      success(res) {
        console.log(res.data);
        let claimant = res.data.claims;
        let isMe = false;
        wx.getStorage({
          key: 'userId',
          success(res) {
            let userId = res.data;
            if (claimant.length > 0) {
              if (claimant[0].account === userId) {
                that.queryPlace(placeId);
                isMe = true;
              }
              that.setData({
                isClaimed: true,
                isMe
              });
            }
          }
        })

      }
    });
  },
  queryPlace(placeId) {
    let that = this;
    api.queryPlaces({
      data: {
        placeId,
        siteId: -1,
        limit: -1,
        pageNumber: 0,
        type: ''
      },
      success(res) {
        console.log(res.data);
        that.setData({
          video: res.data.places[0].vidoUrl
        });
      }
    });
  },
  querySensorData(placeId) {
    let that = this;
    api.querySensorData({
      data: {
        placeIds: placeId,
      },
      success(res) {
        console.log(res.data);
        let newValue = res.data;
        let dataArr = []
        for (const key in newValue) {
          switch (key) {
            case 'par': {
              dataArr.push({
                data: newValue[key].intensity,
                tip: newValue[key].alarmType,
                icon: '/imgs/icon_rayradiation.png',
                title: '光照辐射'
              })
              break
            }
            case 'rainFall': {
              dataArr.push({
                data: newValue[key].rainFall,
                tip: newValue[key].alarmType,
                icon: '/imgs/icon_jiangyu.png',
                title: '降雨量'
              })
              break
            }
            case 'radiation': {
              dataArr.push({
                data: newValue[key].intensity,
                tip: newValue[key].alarmType,
                icon: '/imgs/icon_rayintensity.png',
                title: '光照强度'
              })
              break
            }
            case 'dew': {
              dataArr.push({
                data: newValue[key].temp + '℃',
                tip: newValue[key].alarmType,
                icon: '/imgs/icon_dewtemp.png',
                title: '露水温度'
              })
              break
            }
            case 'soil': {
              dataArr.push({
                data: newValue[key].temp + '℃',
                tip: newValue[key].alarmType,
                icon: '/imgs/icon_soiltemp.png',
                title: '土壤温度'
              })
              dataArr.push({
                data: newValue[key].hum + '%',
                tip: newValue[key].alarmType,
                icon: '/imgs/icon_soilhumidity.png',
                title: '土壤湿度'
              })
              break
            }
            case 'air': {
              dataArr.push({
                data: newValue[key].temp + '℃',
                tip: newValue[key].alarmType,
                icon: '/imgs/icon_airtemp.png',
                title: '空气温度'
              })
              dataArr.push({
                data: newValue[key].hum + '%',
                tip: newValue[key].alarmType,
                icon: '/imgs/icon_airhumidity.png',
                title: '空气湿度'
              })
              break
            }
            case 'wind': {
              dataArr.push({
                data: newValue[key].speed,
                tip: newValue[key].alarmType,
                icon: '/imgs/icon_windspeed.png',
                title: '风速'
              })
              dataArr.push({
                data: newValue[key].direction,
                tip: newValue[key].alarmType,
                icon: '/imgs/icon_winddirection.png',
                title: '风向'
              })
              break
            }
          }
        }
        that.setData({
          sensors: dataArr
        });
      }
    });
  },
  slide(e) {
    this.setData({
      current: e.currentTarget.dataset.index
    });
  },
  change(e) {
    this.setData({
      current: e.detail.current
    });
    this.animation(e.detail.current * 50 + '%');
  },
  animation(index) {
    let animation = wx.createAnimation({
      duration: 400,
      timingFunction: 'ease-in-out',
      delay: 0
    });
    animation.left(index).step();
    this.setData({
      animation: animation.export()
    });
  },
  claim() {
    let that = this;
    let placeId = this.data.placeId;
    let siteId = this.data.siteId;
    let entId = this.data.entId;
    let userId = this.data.userId;
    wx.showModal({
      title: '提示',
      content: '确定认领该地块吗？',
      success(res) {
        if (res.confirm) {
          api.addClaimMsg({
            data: {
              account: res.data,
              placeId,
              siteId,
              entId,
              account: userId
            },
            success(res) {
              wx.showToast({
                title: '认领成功！',
                icon: 'none'
              });
              that.onLoad({
                placeId,
                siteId,
                entId
              });
            }
          });
          api.updatePlaceInfo({
            data: {
              placeId,
              name: '',
              remark: '',
              produce: '',
              good: -1,
              isClaimed: userId
            },
            success(res) {
              console.log(res.data);
            }
          });
        }
      }
    })

  },
  placeIsClaim() {
    let that = this
    api.placeIsClaim({
      data: {
        placeId: that.data.placeId,
        account: that.data.userId
      },
      success(res) {
        console.log(res)
        let msg = res.data.msg
        let type = res.data.type
        if (msg === 'error') {
          var isClaimed = false
        } else {
          var isClaimed = true
          if (type === 1) {
            var isMe = true
          }
        }
        that.setData({
          msg,
          isClaimed,
          isMe
        })
      }
    })
  }
})